package ch.pizzapp.kurier.view;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EViewGroup;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.StringArrayRes;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import ch.pizzapp.kurier.Constants;
import ch.pizzapp.kurier.R;
import ch.pizzapp.kurier.data.GenarateItem;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;

/**
 * Shanuka @Cyberlmj 3:08:30 PM Email: shanuka.gayashan@cyberlmj.com
 */
@EViewGroup(R.layout.quantity_item)
public class QuantityItem extends LinearLayout {

	@ViewById(R.id.textView1)
	TextView textViewQuentity;

	@ViewById(R.id.textViewPrice)
	TextView textViewPrice;

	@ViewById(R.id.buttonAdd)
	ImageButton pulusButtons;

	@ViewById(R.id.buttonMinus)
	ImageButton minusButton;

	@ViewById(R.id.textViewTitle)
	TextView textViewTitle;

	@ViewById(R.id.textViewDescription)
	TextView textViewDescription;

	@ViewById(R.id.imageView)
	ImageView imageView;

	@ViewById(R.id.addToCart)
	Button addToCart;

	@StringArrayRes(R.array.user_setting)
	String[] usersetting;

	Context mContext;

	DisplayImageOptions options;
	ImageLoader imageLoader;

	private Callback mCallback;
	int selectVale = 1;

	@AfterViews
	void afterView() {
		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.white_background)
				.showImageForEmptyUri(R.drawable.white_background)
				.showImageOnFail(R.drawable.white_background).cacheOnDisc(true)
				.displayer(new SimpleBitmapDisplayer())
				.imageScaleType(ImageScaleType.IN_SAMPLE_INT)
				.bitmapConfig(Bitmap.Config.RGB_565).build(); // default;
		imageLoader = ImageLoader.getInstance();

		minusButton.setEnabled(false);
		
		
	}

	public void setOnCallBack(Callback callback) {
		mCallback = callback;

	}

	public QuantityItem(final Context context) {
		super(context);

	}

	public interface Callback {
		void onClicked(int quentity, Double price);
		
		void addToCart();

	}

	private GenarateItem genarateItem;

	@Click({ R.id.buttonAdd, R.id.buttonMinus ,R.id.addToCart})
	void onClick(View vuew) {
		switch (vuew.getId()) {
		case R.id.buttonAdd:

			puls();
			break;
		case R.id.buttonMinus:

			minus();
			break;
			
		case R.id.addToCart:
		
			mCallback.addToCart();
			break;

		default:
			break;
		}
	}

	public QuantityItem(final Context context, final AttributeSet attrs) {
		super(context, attrs);
	}

	public void bind(final GenarateItem genarateItem) {

		this.genarateItem = genarateItem;

		textViewQuentity.setText(String.valueOf(genarateItem.getQuentity()));

		imageLoader.displayImage(Constants.IMAGE_URL + genarateItem.getIcon(),
				imageView, options);
		textViewDescription.setText(genarateItem.getDesc());

		textViewTitle.setText(genarateItem.getTitle());

		if (genarateItem.getCalculatedValue().equals(0.0)) {
			textViewPrice.setText(genarateItem.getPrice());

		} else {
			textViewPrice.setText(String.valueOf(genarateItem
					.getCalculatedValue()));
		}
		
		

	}

	void minus() {

		if (selectVale > 1) {
			selectVale = selectVale - 1;
			textViewQuentity.setText(String.valueOf(selectVale));
			// mCallback.onClicked(selectVale);
			calculatePrice(selectVale);
			buttonEnable(selectVale);
		}

	}

	void puls() {

		if (selectVale < 99) {
			selectVale = selectVale + 1;
			textViewQuentity.setText(String.valueOf(selectVale));
			// mCallback.onClicked(selectVale);
			calculatePrice(selectVale);
			buttonEnable(selectVale);
		}
	}

	void buttonEnable(int selectVale) {
		switch (selectVale) {
		case 1:
			minusButton.setEnabled(false);

			break;
		case 99:
			pulusButtons.setEnabled(false);
			break;

		default:
			pulusButtons.setEnabled(true);
			minusButton.setEnabled(true);
			break;
		}
	}

	void calculatePrice(int value) {

		Double calculatedValue = Double.valueOf(genarateItem.getPrice())
				* value;

		textViewPrice.setText(String.valueOf(calculatedValue));

		mCallback.onClicked(value, calculatedValue);
	}
}
